package stu.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import stu.aistar.dao.ISeatsDao;
import stu.aistar.dao.ISessionDao;
import stu.aistar.model.pojo.Result;
import stu.aistar.model.pojo.Seats;

import stu.aistar.model.pojo.Session;
import stu.aistar.model.vo.SeatVo;
import stu.aistar.service.ISeatsService;


import java.util.List;



@Controller
@RequestMapping("/seat")
public class SeatController {

    private SeatVo seatVos;

    private List<Seats> seatsList;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISeatsService seatsService;
    @Autowired
    private ISeatsDao seatsDao;
    @Autowired
    private ISessionDao sessionDao;

    @GetMapping("/moviedetails")
    public String movieDetails(){
        //return "/WEB-INF/404.jsp";
        return "movie-details";
    }

    @GetMapping("/movieseat")
    public String movieSeatPlans(){
        //return "/WEB-INF/404.jsp";
        return "movie-seat-plan";
    }
    @GetMapping("/seatsuccess")
    @ResponseBody
    public Result seat(Model model, SeatVo seatVo){

        seatVos=seatVo;
        return new Result("200","选座成功",seatVo);
    }

    @GetMapping("/play")
    public String play(Model model){
        System.out.println(seatVos.getSeatArea());
        if(seatVos.getSeatArea().equals("包场")){
            seatsService.updateStateAll(1,1,Integer.valueOf(seatVos.getSession()));
        }else{
            String arr[]=seatVos.getSeatArea().split(",");
            for (String s : arr) {
                System.out.println(s);
            }
            for(int i=0;i< arr.length;i++){
                Integer row=arr[i].charAt(0)-'0';
                System.out.println("hang:"+row);
                Integer col=arr[i].charAt(2)-'0';
                System.out.println("lie:"+col);
                for (Seats seats : seatsList) {
                    if(row==seats.getRow()&&col==seats.getCol()){
                        seatsService.updateStateSelected(seats.getId());
                    }
                }
            }

        }

        model.addAttribute("seat",seatVos);
        return "movie-checkout";
    }

    @GetMapping("/plan")
    public String plan(Model model, String session){
        seatsList=seatsDao.findSeatsBySessionId(Integer.valueOf(session));
        List<Session> sessionList=sessionDao.findSessionByMovieIdAndCinameIdAndHallId(1,1,1);
        model.addAttribute("seatsinit",seatsList);
        model.addAttribute("selected",session);
        model.addAttribute("sessionList",sessionList);

        return "movie-seat-plan";
    }

}
